package cn.holy.app.sztxc.login.view;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.ViewModel;
import android.text.TextUtils;
import android.widget.Toast;

import com.alibaba.android.arouter.launcher.ARouter;

import javax.inject.Inject;

import cn.holy.app.sztxc.app.App;
import cn.holy.app.sztxc.domain.LoginFormBean;
import cn.holy.app.sztxc.domain.UserInfoBean;
import cn.holy.app.sztxc.net.callback.LoadingCallback;
import cn.holy.app.sztxc.repository.UserRepository;

/**
 * Created by xionghao on 2018/2/23.
 */

public class LoginViewModel extends ViewModel {
    private LiveData<UserInfoBean> user;
    private UserRepository userRepo;

    @Inject // UserRepository parameter is provided by Dagger 2
    public LoginViewModel(UserRepository userRepo) {
        this.userRepo = userRepo;
    }

    public void init() {
        if (this.user != null) {
            // ViewModel is created per Fragment so
            // we know the userId won't change
            return;
        }
        user = userRepo.getUser();
    }

    /**
     * 登录操作
     * @param username 账号
     * @param password 密码
     * @param checked 是否保存账号
     */
    public void login(String username, String password, boolean checked,LoadingCallback<UserInfoBean> callback) {

        if(TextUtils.isEmpty(username)){
            Toast.makeText(App.getApp(),"账号不能为空",Toast.LENGTH_SHORT).show();
            return;
        }

        if (checked) {
            userRepo.saveLoginInfo(username, password);
        }
        ARouter.getInstance().build("/app/main").navigation();
        callback.onSuccess(null);
//        userRepo.doLogin(username, password, new LoadingCallback<UserInfoBean>() {
//            @Override
//            public void onStart() {
//
//            }
//
//            @Override
//            public void onError(String msg) {
//
//            }
//
//            @Override
//            public void onSuccess(UserInfoBean userInfoBean) {
//
//            }
//
//            @Override
//            public void onCompleted() {
//
//            }
//        });
    }


    public LoginFormBean loadLoginInfo(){
        return userRepo.loadLoginInfo();
    }


    public LiveData<UserInfoBean> getUser() {
        return this.user;
    }
}
